package com.zixuan.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zixuan.common.Result;
import com.zixuan.common.utils.Constant;
import com.zixuan.dao.EmployeeDao;
import com.zixuan.entity.EmployeeEntity;
import com.zixuan.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeDao employeeDao;
    @Override
    public Result addEmployee(EmployeeEntity employee) {

      if(StringUtils.isEmpty(employee.getEmName())){
          throw new RuntimeException("姓名不能为空");
      }

      if(StringUtils.isEmpty(employee.getUsername())){
          throw new RuntimeException("账号不能为空");
      }

        if(StringUtils.isEmpty(employee.getPassword())){
            throw new RuntimeException("密码不能为空");
        }

        return employeeDao.insert(employee)>0 ? Result.ok() :Result.err();
    }

    @Override
    public Result delEmployeeById(String id) {
        //先去查询这个id是否存在  存在即删除
        EmployeeEntity employee = employeeDao.selectById(id);
        if(employee==null){
          throw new RuntimeException("当前员工不存在！操作失败");
        }
        employeeDao.deleteById(id);
        return Result.ok();
    }

    @Override
    public Result delEmployeeById(String[] ids) {
        for (String id : ids) {

            //先去查询这个id是否存在  存在即删除
            EmployeeEntity employee = employeeDao.selectById(id);
            if(employee==null){
                throw new RuntimeException("当前员工不存在！操作失败");
            }
            employeeDao.deleteById(id);
        }
        return Result.ok();
    }

    @Override
    public Result getEmployeeBy(String id) {

        EmployeeEntity employee = employeeDao.selectById(id);
        if(employee==null){
            throw new RuntimeException("当前员工不存在");
        }
        return new Result(200,true,"SUCCESS",employee);
    }

    @Override
    public Result putEmployee(EmployeeEntity employeeEntity) {
        int update = employeeDao.updateById(employeeEntity);

        if(update==0){
            throw new RuntimeException("服务器出现了问题");
        }

        return new Result(200,true,"SUCCESS");
    }

    @Override
    public Result findAllEmployee(Integer start, Integer end) {
        IPage<EmployeeEntity> page = new Page<>(start,end);
        IPage<EmployeeEntity> iPage = employeeDao.selectPage(page, null);
        iPage.setTotal(page.getTotal());
        return new Result(200,true,"SUCCESS",iPage);
    }


}
